import axios from "axios";
import { ElMessage } from "element-plus";
import { useCounterStore } from "@/stores/counter"
import { useRoute, useRouter } from 'vue-router';
import router from '@/router';

// const Router = useRouter();
// const Route = useRoute();
// const store = useCounterStore();
//读声明
const Read = axios.create({

    baseURL: "https://localhost:7120/api",
    timeout: 5000
})

//请求的拦截器
Read.interceptors.request.use(
    config => {
        const store = useCounterStore();
        config.headers["Authorization"] = "Bearer " + store.CurrentLogin.token;
        return config
    },
    error => {
        console.log("aaaaaaaa");
        const store = useCounterStore();
        if (store.CurrentLogin.token == "") {
            ElMessage.error("您还没有授权,请重新登录")
            setTimeout(() => {
                location.href = "/"
            }, 2000)
        }
        return Promise.reject(error);
    }
)
//响应的拦截器
Read.interceptors.response.use(
    res => {
        const store = useCounterStore();
        res.headers["Authorization"] = "Bearer " + store.CurrentLogin.token;
        return res
    },
    error => {
        const store = useCounterStore();
        if (store.CurrentLogin.token == "") {
            ElMessage.error("您还没有授权,请重新登录")
            setTimeout(() => {
                location.href = "/"
            }, 2000)
        }
        return Promise.reject(error);
    }
)
//读声明
const Write = axios.create({

    baseURL: "http://localhost:5228/api",
    timeout: 5000
})

//请求的拦截器
Write.interceptors.request.use(
    config => {
        const store = useCounterStore();
        config.headers["Authorization"] = "Bearer " + store.CurrentLogin.token;
        return config
    },
    error => {
        console.log("aaaaaaaa");
        const store = useCounterStore();
        if (store.CurrentLogin.token == "") {
            ElMessage.error("您还没有授权,请重新登录")
            setTimeout(() => {
                location.href = "/"
            }, 2000)
        }
        return Promise.reject(error);
    }
)
//响应的拦截器
Write.interceptors.response.use(
    res => {
        const store = useCounterStore();
        res.headers["Authorization"] = "Bearer " + store.CurrentLogin.token;
        return res
    },
    error => {
        const store = useCounterStore();
        if (store.CurrentLogin.token == "") {
            ElMessage.error("您还没有授权,请重新登录")
            setTimeout(() => {
                location.href = "/"
            }, 2000)
        }
        return Promise.reject(error);
    }
)
export { Read, Write }